/*
 * @(#)RePlugin.java 2020年3月2日 下午8:59:03
 * Copyright 2020 zmr, Inc. All rights reserved. 
 * PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.wind.cqboot.plugin;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

import net.lz1998.pbbot.bot.Bot;
import net.lz1998.pbbot.bot.BotPlugin;
import onebot.OnebotEvent;

/**
 * <p>File：RePlugin.java</p>
 * <p>Title: </p>
 * <p>Description:</p>
 * <p>Copyright: Copyright (c) 2020 2020年3月2日 下午8:59:03</p>
 * <p>Company: zmrit.com </p>
 * @author zmr
 * @version 1.0
 */
@Component
public class RepeatPlugin extends BotPlugin
{
    // Ctrl+O
    Map<Long, String>  lastMsgMap = new ConcurrentHashMap<>();

    Map<Long, Integer> countMap   = new ConcurrentHashMap<>();

    @Override
    public int onGroupMessage(@NotNull Bot bot,
            @NotNull OnebotEvent.GroupMessageEvent event)
    {
        String msg = event.getRawMessage();
        Long groupId = event.getGroupId();
        String lastMsg = lastMsgMap.getOrDefault(groupId, "");
        Integer count = countMap.getOrDefault(groupId, 0);
        if (msg.equals(lastMsg))
        {
            count++;
            countMap.put(groupId, count);
            if (count > 2)
            {
                bot.sendGroupMsg(groupId, "刷屏会得到一张机票", false);
                countMap.put(groupId, 0);
            }
        }
        else
        {
            countMap.put(groupId, 1);
            lastMsgMap.put(groupId, msg);
        }
        return super.onGroupMessage(bot, event);
    }
}
